<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>编辑健康指标</title>
    <style>
        .form-container {
            max-width: 500px;
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
        }
        input, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
        }
        .btn:hover {
            background-color: #45a049;
        }
        h2 {
            text-align: center;
            color: #333;
        }
    </style>
</head>
<body>
<div class="form-container">
    <h2>编辑健康指标记录</h2>
    <form action="<%=request.getContextPath()%>/healthmetrics/edit" method="post">
        <input type="hidden" name="metricsid" value="${metrics.metricsid}">

        <div class="form-group">
            <label for="userid">用户ID</label>
            <input type="number" id="userid" name="userid" value="${metrics.userid}" required>
        </div>

        <div class="form-group">
            <label for="metricstype">指标类型</label>
            <select id="metricstype" name="metricstype" required>
                <option value="weight" ${metrics.metricstype == 'weight' ? 'selected' : ''}>体重</option>
                <option value="bloodpressure" ${metrics.metricstype == 'bloodpressure' ? 'selected' : ''}>血压</option>
                <option value="heartrate" ${metrics.metricstype == 'heartrate' ? 'selected' : ''}>心率</option>
            </select>
        </div>

        <div class="form-group">
            <label for="value">指标数值</label>
            <input type="text" id="value" name="value" value="${metrics.value}" required>
        </div>

        <div class="form-group">
            <label for="unit">单位</label>
            <input type="text" id="unit" name="unit" value="${metrics.unit}" required>
        </div>

        <div class="form-group">
            <label for="createtime">记录日期</label>
            <input type="date" id="createtime" name="createtime" value="${metrics.createtime}" required>
        </div>

        <button type="submit" class="btn">更新记录</button>
    </form>
</div>
</body>
<script>
    document.querySelector('form').addEventListener('submit', function(e) {
        const dateVal = document.getElementById('createtime').value;
        if (!/^\d{4}-\d{2}-\d{2}$/.test(dateVal)) {
            alert('日期格式必须为YYYY-MM-DD');
            e.preventDefault();
            return false;
        }

        // 验证数字字段
        const numFields = ['userid', 'metricsid'];
        for (let field of numFields) {
            if (isNaN(document.getElementById(field).value)) {
                alert(field + '必须为数字');
                e.preventDefault();
                return false;
            }
        }
        return true;
    });
</script>
</html>